package trees;

import java.util.Arrays;
import java.util.List;
import java.util.Stack;

public class EvalRPN {
    public int evalRPN(String[] tokens) {
        Stack<String> number = new Stack<>();

        List<String> characterList = Arrays.asList("+", "-", "*", "/");

        for (String token : tokens) {
            if (characterList.contains(token)) {
                int second = Integer.parseInt(number.pop());
                int first = Integer.parseInt(number.pop());

                switch (token) {
                    case "+":
                        number.push(String.valueOf(first + second));
                        break;
                    case "-":
                        number.push(String.valueOf(first - second));
                        break;
                    case "*":
                        number.push(String.valueOf(first * second));
                        break;
                    default:
                        number.push(String.valueOf(first / second));
                        break;
                }
            } else {
                number.push(token);
            }
        }

        return Integer.parseInt(number.pop());
    }

    public static void main(String[] args) {
        EvalRPN rpn = new EvalRPN();
        String[] tokens = new String[]{"10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"};
        System.out.println(rpn.evalRPN(tokens));
    }
}
